﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;
using IFactory;

public partial class QuarterPlan_QuarterPlanAddAdmin : System.Web.UI.Page
{
    DepartmentInfoManager dllDepartment = new DepartmentInfoManager();
    UserInfoManager dllUserInfo = new UserInfoManager();
    QuarterPlanofManager dllQuarterPlanofManager = new QuarterPlanofManager();
    Authentication dllAuthentication = new Authentication();
    VerifyCircuitManager dllVerifyCircuitManager = new VerifyCircuitManager();
    QuarterDateManager dllQuarterDateManager = new QuarterDateManager();
    QuarterPlanDetailManager dllQuarterPlanDetail = new QuarterPlanDetailManager();
    Model.UserInfo objUserInfo = new Model.UserInfo();
    DataTable _powerTable = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        this.gdvQuarterPlan.Attributes.Add("style", "word-break:keep-all;word-wrap:normal");
        this.gdvQuarterPlan.Attributes.Add("style", "word-break:break-all;word-wrap:break-word");
        if (!IsPostBack)
        {
            bindQuarterPlan();
            bindDropDownList(this.drpYear);
            bindDropDownList(this.drpDepartment);
            bindDropDownList(this.drpQuarterDate);
            //默认绑定责任部门文本框
            //this.txtMasterDepartment.Text = this.drpDepartment.SelectedItem.Text;
        }
    }



    /// <summary>
    /// 创建空DataTable用于绑定GridView
    /// </summary>
    /// <author>grokyao</author>
    /// <date>2009-2-16</date>
    DataTable createDataTable()
    {
        DataTable _dataTable = new DataTable();
        _dataTable.Columns.Add("QuarterPlanId");
        _dataTable.Columns.Add("QuarterPlanSortId");
        _dataTable.Columns.Add("StartTime");
        _dataTable.Columns.Add("CompleteTime");
        _dataTable.Columns.Add("WorkItem");
        _dataTable.Columns.Add("MasterDepartment");
        _dataTable.Columns.Add("MasterPeople");
        _dataTable.Columns.Add("SlaveDepartment");
        _dataTable.Columns.Add("SlaveDepartmentDuty");
        _dataTable.Columns.Add("EvaluatePeople");
        _dataTable.Columns.Add("Remart");
        return _dataTable;
    }

    /// <summary>
    /// 绑定空记录表头GridView
    /// </summary>
    /// <author>grokyao</author>
    /// <date>2009-2-16</date>
    void bindQuarterPlan()
    {
        DataTable _quareterPlan = createDataTable();
        DataRow dr = _quareterPlan.NewRow();

        _quareterPlan.Rows.Add(dr);
        DataView dv = new DataView(_quareterPlan);
        dv.Sort = "QuarterPlanSortId";
        this.gdvQuarterPlan.DataSource = dv;
        this.gdvQuarterPlan.DataBind();
        //隐藏首行，只保留表头
        this.gdvQuarterPlan.Rows[0].Visible = false;
    }

    /// <summary>
    /// 绑定DropDownList
    /// </summary>
    /// <author>grokyao</author>
    /// <date>2009-2-16</date>
    void bindDropDownList(DropDownList _drp)
    {
        if (_drp.Equals(this.drpDepartment))
        {
            _drp.DataSource = dllDepartment.getNoTree();
            _drp.DataTextField = "DepartmentName";
            _drp.DataValueField = "DepartmentId";
            _drp.DataBind();
        }
        if (_drp.Equals(this.drpYear))
        {
            for (int i = 2009; i < 2100; i++)
            {
                ListItem li = new ListItem(i.ToString(), i.ToString());
                _drp.Items.Add(li);
            }
            //根据系统日期自动绑定到当前年份
            //string _year = DateTime.Now.Year.ToString();
            //this.drpYear.SelectedIndex = this.drpYear.Items.IndexOf(this.drpYear.Items.FindByText(_year));
            //根据系统当前日期查找到对应的年周期
            DataTable _year = dllQuarterDateManager.getQuarterDate();
            this.drpYear.SelectedIndex = this.drpYear.Items.IndexOf(this.drpYear.Items.FindByText(_year.Rows[0][2].ToString()));

        }
        if (_drp.Equals(this.drpQuarterDate))
        {
            object[] _value = new object[1];
            _value[0] = this.drpYear.SelectedValue;
            DataTable _quarter = dllQuarterDateManager.get(QuarterDateManager.field.QuarterYear.ToString(), _value);
            if (_quarter.Rows.Count != 0)
            {
                this.drpQuarterDate.DataSource = _quarter;
                this.drpQuarterDate.DataTextField = "QuarterName";
                this.drpQuarterDate.DataValueField = "Quarter_Id";
                this.drpQuarterDate.DataBind();
                #region dropdownlist显示系统所对应的当前年周期
                //根据系统当前日期查找到对应的年周期
                DataTable _quartertime = dllQuarterDateManager.getQuarterDate();
                //如果查到结果（结果只有一条）

                if (_quartertime.Rows.Count == 1)
                {
                    this.drpQuarterDate.SelectedIndex = this.drpQuarterDate.Items.IndexOf(this.drpQuarterDate.Items.FindByText(_quartertime.Rows[0][1].ToString()));
                    this.drpQuarterDate.DataSource = _quartertime;
                    this.drpQuarterDate.DataTextField = "QuarterName";
                    this.drpQuarterDate.DataValueField = "Quarter_Id";
                }
                #endregion

            }
            else
            {
                ListItem li = new ListItem("无年周期", "0");
                this.drpQuarterDate.Items.Clear();
                this.drpQuarterDate.Items.Add(li);
            }

        }
    }


    /// <summary>
    /// 判断控件值是否为空
    /// </summary>
    /// <returns>空返回0，有值返回1</returns>
    /// <author>grokyao</author>
    /// <date>2009-2-17</date>
    int isNull()
    {
        #region 验证是否为空
        if (this.txtQuarterPlanSortId.Value.Length == 0)
        {
            return 0;
        }
        if (this.txtCompleteTime.Text.Length == 0)
        {
            return 0;
        }
        if (this.txtMasterPeople.Value.Length == 0)
        {
            return 0;
        }
        if (this.txtWorkItem.Text.Length == 0)
        {
            return 0;
        }
        if (this.txtSlaveDepartment.Value.Length == 0)
        {
            return 0;
        }
        if (this.txtSlaveDepartment.Value != "独立完成")
        {
            ////if (this.txtSlaveDepartmentDuty.Text.Length < 18)
            ////{
                return 0;
            //}
        }
        if (this.txtStartTime.Text.Length == 0)
        {
            return 0;
        }
        if (this.txtVerifyPeople.Value.Length == 0)
        {
            return 0;
        }
        return 1;
        #endregion
    }

    /// <summary>
    /// 时间验证
    /// </summary>
    bool verifyDate()
    {
        #region 必要验证判断
        //判断完成时间是否大于起始时间

        DateTime _startTime = Convert.ToDateTime(this.txtStartTime.Text.Trim());
        DateTime _endTime = Convert.ToDateTime(this.txtCompleteTime.Text.Trim());
        if (!dllAuthentication.IsEndTimeGreaterThanStartTime(_startTime, _endTime))
        {
            Utility.HttpHelp.alert("完成时间需要大于起始时间", Page);
            return false;
        }


        return true;
        #endregion
    }


    /// <summary>
    /// 根据逗号换行事件
    /// </summary>
    void wrapByComma(GridView _gridView, int _rowIndex, int _cells)
    {
        string _content = _gridView.Rows[_rowIndex].Cells[_cells].Text;
        _gridView.Rows[_rowIndex].Cells[_cells].Text = _content.Replace("&lt;br&gt;", "<br>");
        //_gridView.Rows[_rowIndex].Cells[_cells].Text = _content.Replace("&gt;", ">");
        _gridView.Rows[_rowIndex].Cells[_cells].Text = _gridView.Rows[_rowIndex].Cells[_cells].Text.Replace(",", "<br>");


    }


    /// <summary>
    /// 保存为草稿
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>d
    /// <author>grokyao</author>
    /// <date>2009-2-16</date>
    protected void btnSave_Click(object sender, EventArgs e)
    {
        //排除之前设定的空行
        if (this.gdvQuarterPlan.Rows.Count > 1)
        {
            if (this.gdvQuarterPlan.DataKeys[1]["QuarterPlanId"].ToString().Length == 0 || this.gdvQuarterPlan.DataKeys[1]["QuarterPlanId"].ToString() == "&nbsp;")
            {
                //添加年计划入库
                addQuarterPlan(0);
                //读取该年度该部门下所有年计划（带有id）并绑定
                object[] _value = new object[1];
                _value[0] = Convert.ToInt32(this.drpQuarterDate.SelectedValue);
                DataTable _quarterPlan = dllQuarterPlanofManager.get(QuarterPlanofManager.field.PlanSendState.ToString(), _value);
                DataRow dr = _quarterPlan.NewRow();
                _quarterPlan.Rows.InsertAt(dr, 0);
                DataView dv = new DataView(_quarterPlan);
                dv.Sort = "QuarterPlanSortId";
                if (_quarterPlan.Rows.Count > 1)
                {
                    this.gdvQuarterPlan.DataSource = dv;
                    this.gdvQuarterPlan.DataBind();
                }
                for (int i = 0; i < this.gdvQuarterPlan.Rows.Count; i++)
                {
                    wrapByComma(this.gdvQuarterPlan, i, 6);
                    wrapByComma(this.gdvQuarterPlan, i, 7);
                }
                this.gdvQuarterPlan.Rows[0].Visible = false;
            }
            else
            {
                Utility.HttpHelp.alert("计划已保存！", Page);
            }
        }
        else
        {
            Utility.HttpHelp.alert("没有要保存的年计划", Page);
            return;
        }
        //清除全选按钮状态
        this.ckbAllSelect.Checked = false;
    }


    /// <summary>
    /// 年计划提交审核，但年计划必须被分解才可提交！
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    /// <author>grokyao</author>
    /// <date>2009-2-16</date>
    protected void btnOk_Click(object sender, EventArgs e)
    {
        //排除之前设定的空行
        if (this.gdvQuarterPlan.Rows.Count > 1)
        {
            if (this.gdvQuarterPlan.DataKeys[1]["QuarterPlanId"].ToString().Length == 0 || this.gdvQuarterPlan.DataKeys[1]["QuarterPlanId"].ToString() == "&nbsp;")
            {
                addQuarterPlan(3);
                bindQuarterPlan();
                #region 善后操作
                //将文本框清空
                this.txtQuarterPlanSortId.Value = "";
                this.txtCompleteTime.Text = "";
                this.txtMasterPeople.Value = "";
                this.txtRemark.Text = "";
                this.txtSlaveDepartment.Value = "";
                this.txtSlaveDepartmentDuty.Text = "";
                this.txtStartTime.Text = "";
                this.txtVerifyPeople.Value = "";
                this.txtWorkItem.Text = "";
                #endregion
            }
            else
            {
                editQuarterPlan();
            }
        }
        else
        {
            Utility.HttpHelp.alert("请先保存年计划", Page);
            return;
        }

        //清除全选按钮状态
        this.ckbAllSelect.Checked = false;

        Utility.HttpHelp.closeWindow("QuarterPlanDisplay.aspx?Id=" + this.drpQuarterDate.SelectedValue + "&depart=" + this.drpDepartment.SelectedValue, Page);
    }

    /// <summary>
    /// 添加年计划方法
    /// </summary>
    /// <param name="state">0草稿 1 提交</param>
    /// <author>grokyao</author>
    /// <date>2009-2-5</date>
    void addQuarterPlan(int state)
    {
        #region 参数
        int _count = 0;
        List<object[]> _valueList = new List<object[]>();
        //得到参数 部门名称的DataTable
        object[] _valueDepartmentName = new object[1];
        _valueDepartmentName[0] = this.drpDepartment.SelectedItem.Text;
        DataTable _VerifyCircuitId = dllVerifyCircuitManager.getVerifyCircuitIdByDepartment(_valueDepartmentName);
        if (_VerifyCircuitId.Rows.Count == 0)
        {
            Utility.Error.RedirectErrorPage("所在部门无计划流程，请重试");
        }
        //获取登录用户名
        object[] _user = new object[1];
        _user[0] = this.txtSponsor.Value;
        DataTable _usertable = dllUserInfo.get(UserInfoManager.field.RealName.ToString(), _user);
        if (_usertable.Rows.Count == 0)
        {
            return;
        }
        else
        {
            for (int i = 1; i < this.gdvQuarterPlan.Rows.Count; i++)
            {
                CheckBox _ckb = (CheckBox)this.gdvQuarterPlan.Rows[i].Cells[0].FindControl("ckbSelect");
                if (_ckb.Checked == true)
                {
                    _count++;
                    object[] _value = new object[16];
                    //开始时间
                    _value[0] = Convert.ToDateTime(this.gdvQuarterPlan.Rows[i].Cells[2].Text.Trim());
                    //完成时间
                    _value[1] = Convert.ToDateTime(this.gdvQuarterPlan.Rows[i].Cells[3].Text.Trim());
                    //工作内容
                    _value[2] = this.gdvQuarterPlan.Rows[i].Cells[4].Text.Trim();
                    //责任部门
                    _value[3] = this.drpDepartment.SelectedItem.Text;
                    //责任人
                    _value[4] = this.gdvQuarterPlan.Rows[i].Cells[5].Text.Trim().Replace("<br>", ",");
                    //配合部门
                    _value[5] = this.gdvQuarterPlan.Rows[i].Cells[6].Text.Trim().Replace("<br>", ",");
                    //配合部门责任
                    _value[6] = this.gdvQuarterPlan.Rows[i].Cells[7].Text.Trim();
                    _value[7] = state;//发送状态
                    _value[8] = 0;//是否达成共识
                    _value[9] = this.gdvQuarterPlan.Rows[i].Cells[9].Text.Trim(); ;//Remart
                    _value[10] = Convert.ToInt32(this.drpQuarterDate.SelectedValue);//QuarterDate
                    _value[11] = Convert.ToInt32(_VerifyCircuitId.Rows[0]["VerifyCircuitId"].ToString());
                    _value[12] = _usertable.Rows[0]["UserName"].ToString();
                    _value[13] = this.gdvQuarterPlan.Rows[i].Cells[8].Text.Trim();
                    _value[14] = Convert.ToInt32(this.gdvQuarterPlan.Rows[i].Cells[1].Text.Trim());
                    _value[15] = "新增";
                    _valueList.Add(_value);
                }
            }
        #endregion
            if (_count <= 0)
            {
                Utility.HttpHelp.alert("请选择要保存的计划", Page);
                return;
            }
            if (_valueList.Count > 0)
            {
                int result = dllQuarterPlanofManager.addWithTransaction(_valueList);
                if (result == 0)
                {
                    Utility.Error.RedirectErrorPage("添加年计划未成功，请重试！");
                }

                #region 善后操作
                //将文本框清空
                this.txtQuarterPlanSortId.Value = this.gdvQuarterPlan.Rows.Count.ToString();
                this.txtCompleteTime.Text = "";
                this.txtMasterPeople.Value = "";
                this.txtRemark.Text = "";
                this.txtSlaveDepartment.Value = "";
                this.txtSlaveDepartmentDuty.Text = "";
                this.txtStartTime.Text = "";
                this.txtVerifyPeople.Value = "";
                this.txtWorkItem.Text = "";
                #endregion
            }
        }
    }

    /// <summary>
    /// 修改年计划方法
    /// </summary>
    /// <author>grokyao</author>
    /// <date>2009-2-5</date>
    void editQuarterPlan()
    {
        #region 参数
        int _count = 0;
        List<object[]> _valueList = new List<object[]>();
        for (int i = 1; i < this.gdvQuarterPlan.Rows.Count; i++)
        {
            CheckBox _ckb = (CheckBox)this.gdvQuarterPlan.Rows[i].Cells[0].FindControl("ckbSelect");
            if (_ckb.Checked == true)
            {
                _count++;
                object[] _value = new object[2];
                _value[0] = Convert.ToInt32(this.gdvQuarterPlan.DataKeys[i]["QuarterPlanId"].ToString());
                _valueList.Add(_value);
            }
        }
        #endregion
        if (_count <= 0)
        {
            Utility.HttpHelp.alert("请选择要提交的计划", Page);
            return;
        }
        if (_valueList.Count > 0)
        {
            int result = dllQuarterPlanofManager.editWithTransaction(QuarterPlanofManager.field.admin.ToString(), _valueList);
            if (result == 0)
            {
                Utility.Error.RedirectErrorPage("提交年计划未成功，请重试！");
            }
            else
            {
                DataTable _quarter = createDataTable();
                DataRow _dr = _quarter.NewRow();
                _quarter.Rows.Add(_dr);
                this.gdvQuarterPlan.DataSource = _quarter;
                this.gdvQuarterPlan.DataBind();
                this.gdvQuarterPlan.Rows[0].Visible = false;
            }

            #region 善后操作
            //将文本框清空
            this.txtQuarterPlanSortId.Value = "";
            this.txtCompleteTime.Text = "";
            this.txtMasterPeople.Value = "";
            this.txtRemark.Text = "";
            this.txtSlaveDepartment.Value = "";
            this.txtSlaveDepartmentDuty.Text = "";
            this.txtStartTime.Text = "";
            this.txtVerifyPeople.Value = "";
            this.txtWorkItem.Text = "";
            #endregion
        }
    }


    /// <summary>
    /// 删除按钮事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    /// <author>grokyao</author>
    /// <date>2009-2-17</date>
    protected void btnDel_Click(object sender, EventArgs e)
    {
        //得到button所在行        
        Button bt = (Button)sender;//获得当前控件
        TableCell cell = (TableCell)bt.Parent;//得到所处的父单元格
        GridViewRow iRow = (GridViewRow)cell.Parent;//得到所处父单元行
        int _rowIndex = iRow.RowIndex;//得到该行的索引
        //遍历GridView将GridView转为DataTable
        DataTable _quarterPlan = createDataTable();
        //获取GridView当前内容导入DataTable
        for (int i = 0; i < this.gdvQuarterPlan.Rows.Count; i++)
        {
            DataRow _drGridView = _quarterPlan.NewRow();

            _drGridView["QuarterPlanSortId"] = this.gdvQuarterPlan.Rows[i].Cells[1].Text;
            _drGridView["StartTime"] = this.gdvQuarterPlan.Rows[i].Cells[2].Text.ToString();
            _drGridView["CompleteTime"] = this.gdvQuarterPlan.Rows[i].Cells[3].Text.ToString();
            _drGridView["WorkItem"] = this.gdvQuarterPlan.Rows[i].Cells[4].Text.ToString();
            _drGridView["MasterDepartment"] = this.gdvQuarterPlan.Rows[i].Cells[5].Text.ToString();
            _drGridView["MasterPeople"] = this.gdvQuarterPlan.Rows[i].Cells[6].Text.ToString();
            _drGridView["SlaveDepartment"] = this.gdvQuarterPlan.Rows[i].Cells[7].Text.ToString();
            _drGridView["SlaveDepartmentDuty"] = this.gdvQuarterPlan.Rows[i].Cells[8].Text.ToString();
            _drGridView["EvaluatePeople"] = this.gdvQuarterPlan.Rows[i].Cells[9].Text.ToString();
            _drGridView["Remart"] = this.gdvQuarterPlan.Rows[i].Cells[10].Text.ToString();

            #region 去除特殊符号如&nbsp;
            _drGridView["QuarterPlanId"] = _drGridView["QuarterPlanId"].ToString().Replace("&", "");
            _drGridView["QuarterPlanId"] = _drGridView["QuarterPlanId"].ToString().Replace("nbsp;", "");
            _drGridView["QuarterPlanSortId"] = _drGridView["QuarterPlanSortId"].ToString().Replace("&", "");
            _drGridView["QuarterPlanSortId"] = _drGridView["QuarterPlanSortId"].ToString().Replace("nbsp;", "");
            _drGridView["StartTime"] = _drGridView["StartTime"].ToString().Replace("&", "");
            _drGridView["StartTime"] = _drGridView["StartTime"].ToString().Replace("nbsp;", "");
            _drGridView["CompleteTime"] = _drGridView["CompleteTime"].ToString().Replace("&", "");
            _drGridView["CompleteTime"] = _drGridView["CompleteTime"].ToString().Replace("nbsp;", "");
            _drGridView["WorkItem"] = _drGridView["WorkItem"].ToString().Replace("&", "");
            _drGridView["WorkItem"] = _drGridView["WorkItem"].ToString().Replace("nbsp;", "");

            _drGridView["MasterDepartment"] = _drGridView["MasterDepartment"].ToString().Replace("&", "");
            _drGridView["MasterDepartment"] = _drGridView["MasterDepartment"].ToString().Replace("nbsp;", "");
            _drGridView["MasterPeople"] = _drGridView["MasterPeople"].ToString().Replace("&", "");
            _drGridView["MasterPeople"] = _drGridView["MasterPeople"].ToString().Replace("nbsp;", "");
            _drGridView["MasterPeople"] = _drGridView["MasterPeople"].ToString().Replace(",<br>", ",");
            _drGridView["SlaveDepartment"] = _drGridView["SlaveDepartment"].ToString().Replace("&", "");
            _drGridView["SlaveDepartment"] = _drGridView["SlaveDepartment"].ToString().Replace("nbsp;", "");
            _drGridView["SlaveDepartment"] = _drGridView["SlaveDepartment"].ToString().Replace(",<br>", ",");

            _drGridView["SlaveDepartmentDuty"] = _drGridView["SlaveDepartmentDuty"].ToString().Replace("&", "");
            _drGridView["SlaveDepartmentDuty"] = _drGridView["SlaveDepartmentDuty"].ToString().Replace("nbsp;", "");
            _drGridView["EvaluatePeople"] = _drGridView["EvaluatePeople"].ToString().Replace("&", "");
            _drGridView["EvaluatePeople"] = _drGridView["EvaluatePeople"].ToString().Replace("nbsp;", "");
            _drGridView["EvaluatePeople"] = _drGridView["EvaluatePeople"].ToString().Replace(",<br>", ",");

            _drGridView["Remart"] = _drGridView["Remart"].ToString().Replace("&", "");
            _drGridView["Remart"] = _drGridView["Remart"].ToString().Replace("nbsp;", "");
            #endregion

            _quarterPlan.Rows.Add(_drGridView);
        }
        _quarterPlan.Rows[_rowIndex].Delete();
        //计划如果被保存为草稿，也同时删除
        if (this.gdvQuarterPlan.DataKeys[_rowIndex]["QuarterPlanId"].ToString() != "&nbsp;" && this.gdvQuarterPlan.DataKeys[_rowIndex]["QuarterPlanId"].ToString().Length > 0)
        {
            object[] _value = new object[1];
            _value[0] = Convert.ToInt32(this.gdvQuarterPlan.DataKeys[_rowIndex]["QuarterPlanId"].ToString());
            int result = dllQuarterPlanofManager.del(QuarterPlanofManager.field.QuarterPlanId.ToString(), _value);
            if (result == 0)
            {
                Utility.Error.RedirectErrorPage("删除年计划未成功，请重试！");
            }
        }
        this.gdvQuarterPlan.DataSource = _quarterPlan;
        this.gdvQuarterPlan.DataBind();
        //隐藏首行，只保留表头
        this.gdvQuarterPlan.Rows[0].Visible = false;
        for (int i = 0; i < this.gdvQuarterPlan.Rows.Count; i++)
        {
            wrapByComma(this.gdvQuarterPlan, i, 6);
            wrapByComma(this.gdvQuarterPlan, i, 7);
        }
    }

    /// <summary>
    /// 分解所选年计划，弹出添加分解窗体
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    /// <author>grokyao</author>
    /// <date>2009-2-17</date>
    protected void btnQuarterResolve_Click(object sender, EventArgs e)
    {
        //计数器
        int _count = 0;
        //年计划id 
        string _quarterPlanId = "";
        string _quarterPlanSortId = "";
        //年计划审核流程id
        int _verifyCircuitId = 0;
        object[] _valueDepartmentName = new object[1];
        _valueDepartmentName[0] = this.drpDepartment.SelectedItem.Text;
        //所在部门
        string _masterDepartment = this.drpDepartment.SelectedItem.Text;
        DataTable _verifyCircuitTable = dllVerifyCircuitManager.getVerifyCircuitIdByDepartment(_valueDepartmentName);
        if (_verifyCircuitTable.Rows.Count == 0)
        {
            Utility.Error.RedirectErrorPage("所在部门无计划流程，请重试");
        }
        else
        {
            _verifyCircuitId = Convert.ToInt32(_verifyCircuitTable.Rows[0]["VerifyCircuitId"].ToString());
        }

        //年计划所在周期
        int _quarterDate = Convert.ToInt32(this.drpQuarterDate.SelectedValue);
        //发起人
        Model.UserInfo objUserInfo = new Model.UserInfo();
        string _sponsor = objUserInfo.UserName;
        //获得所选年计划的id
        for (int i = 1; i < this.gdvQuarterPlan.Rows.Count; i++)
        {
            CheckBox _chk = (CheckBox)this.gdvQuarterPlan.Rows[i].Cells[0].FindControl("ckbSelect");
            if (_chk.Checked == true)
            {
                if (this.gdvQuarterPlan.DataKeys[i]["QuarterPlanId"].ToString().Length > 0)
                {
                    _quarterPlanId = this.gdvQuarterPlan.DataKeys[i]["QuarterPlanId"].ToString();
                    _quarterPlanSortId = this.gdvQuarterPlan.Rows[i].Cells[1].Text;
                }
                _count++;
            }
        }
        if (_quarterPlanId.Length == 0)
        {
            Utility.HttpHelp.alert("计划提交或保存后方可分解计划", Page);
            return;
        }
        if (_count != 1)
        {
            Utility.HttpHelp.alert("只能选择一个年计划", Page);
            return;
        }
        else
        {
            //定向到添加年分解页面
            string _url = "QuarterResolveAdd.aspx?_quarterPlanId=" + _quarterPlanId + "&_quarterPlanSortId=" + _quarterPlanSortId;
            Page.RegisterStartupScript("windowsopen", "<script language=javascript>window.open('" + _url + "','quarterplandetailmodify','height=200,width=900,top=300,left=80,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no');</script>");
        }
        if (_count == 0)
        {
            Utility.HttpHelp.alert("请选择一个年计划", Page);

        }
    }



    /// <summary>
    /// 全选事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    /// <author>grokyao</author>
    /// <date>2009-2-19</date>
    protected void ckbAllSelect_CheckedChanged(object sender, EventArgs e)
    {
        if (this.ckbAllSelect.Checked == true)
        {
            for (int i = 0; i < this.gdvQuarterPlan.Rows.Count; i++)
            {
                CheckBox _chk = ((CheckBox)this.gdvQuarterPlan.Rows[i].Cells[0].FindControl("ckbSelect"));
                _chk.Checked = true;
            }
        }
        else
        {
            for (int i = 0; i < this.gdvQuarterPlan.Rows.Count; i++)
            {
                CheckBox _chk = ((CheckBox)this.gdvQuarterPlan.Rows[i].Cells[0].FindControl("ckbSelect"));
                _chk.Checked = false;
            }
        }

    }


    /// <summary>
    /// 分页事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gdvQuarterPlan_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gdvQuarterPlan.PageIndex = e.NewPageIndex;
        object[] _value = new object[1];
        _value[0] = Convert.ToInt32(this.drpQuarterDate.SelectedValue);
        DataTable _quarterPlan = dllQuarterPlanofManager.get(QuarterPlanofManager.field.QuarterDate.ToString(), _value);
        DataRow dr = _quarterPlan.NewRow();
        _quarterPlan.Rows.InsertAt(dr, 0);
        this.gdvQuarterPlan.DataSource = _quarterPlan;
        this.gdvQuarterPlan.DataBind();
        for (int i = 0; i < this.gdvQuarterPlan.Rows.Count; i++)
        {
            wrapByComma(this.gdvQuarterPlan, i, 6);
            wrapByComma(this.gdvQuarterPlan, i, 7);
        }
        this.gdvQuarterPlan.Rows[0].Visible = false;
    }
    /// <summary>
    /// 年份索引改变事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindDropDownList(this.drpQuarterDate);
    }

    /// <summary>
    /// 增加一行
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAdd_Click1(object sender, EventArgs e)
    {
        int _isnull = 1;//isNull();
        if (_isnull == 0)
        {
            Utility.HttpHelp.alert("请输入完整", Page);
            return;
        }
        if (this.txtSponsor.Value.Length == 0)
        {
            Utility.HttpHelp.alert("请选择发起人", Page);
            return;
        }
        if (this.txtSponsor.Value.Contains(","))
        {
            Utility.HttpHelp.alert("发起人只能选择一位", Page);
            return;
        }
        if (this.txtSlaveDepartment.Value != "独立完成")
        {
            if (this.txtSlaveDepartmentDuty.Text.Length < 18)
            {
                Utility.HttpHelp.alert("配合部门责任字数不得低于32个字符", Page);
                return;
            }
        }


        if (Convert.ToInt32(this.txtQuarterPlanSortId.Value) == 0)
        {
            Utility.HttpHelp.alert("计划编号只能从1开始！", Page);
            return;
        }
        if (!verifyDate())
        {
            return;
        }
        if (this.txtMasterPeople.Value.Contains(","))
        {
            Utility.HttpHelp.alert("责任人不可为多人！", Page);
            return;
        }
        if (this.txtVerifyPeople.Value.Contains(","))
        {
            Utility.HttpHelp.alert("评估人不可为多人！", Page);
            return;
        }
        DataTable _quarterPlan = createDataTable();
        //获取GridView当前内容导入DataTable
        for (int i = 0; i < this.gdvQuarterPlan.Rows.Count; i++)
        {
            DataRow _drGridView = _quarterPlan.NewRow();
            _drGridView["QuarterPlanId"] = "";
            _drGridView["QuarterPlanSortId"] = this.gdvQuarterPlan.Rows[i].Cells[1].Text.ToString();
            _drGridView["StartTime"] = this.gdvQuarterPlan.Rows[i].Cells[2].Text.ToString();
            _drGridView["CompleteTime"] = this.gdvQuarterPlan.Rows[i].Cells[3].Text.ToString();
            _drGridView["WorkItem"] = this.gdvQuarterPlan.Rows[i].Cells[4].Text.ToString();
            _drGridView["MasterDepartment"] = this.drpDepartment.SelectedItem.Text;
            _drGridView["MasterPeople"] = this.gdvQuarterPlan.Rows[i].Cells[5].Text.ToString();
            _drGridView["SlaveDepartment"] = this.gdvQuarterPlan.Rows[i].Cells[6].Text.ToString();
            _drGridView["SlaveDepartmentDuty"] = this.gdvQuarterPlan.Rows[i].Cells[7].Text.ToString();
            _drGridView["EvaluatePeople"] = this.gdvQuarterPlan.Rows[i].Cells[8].Text.ToString();
            _drGridView["Remart"] = this.gdvQuarterPlan.Rows[i].Cells[9].Text.ToString();

            #region 去除特殊符号如&nbsp;
            _drGridView["QuarterPlanId"] = _drGridView["QuarterPlanId"].ToString().Replace("&", "");
            _drGridView["QuarterPlanId"] = _drGridView["QuarterPlanId"].ToString().Replace("nbsp;", "");
            _drGridView["QuarterPlanSortId"] = _drGridView["QuarterPlanSortId"].ToString().Replace("&", "");
            _drGridView["QuarterPlanSortId"] = _drGridView["QuarterPlanSortId"].ToString().Replace("nbsp;", "");
            _drGridView["StartTime"] = _drGridView["StartTime"].ToString().Replace("&", "");
            _drGridView["StartTime"] = _drGridView["StartTime"].ToString().Replace("nbsp;", "");
            _drGridView["CompleteTime"] = _drGridView["CompleteTime"].ToString().Replace("&", "");
            _drGridView["CompleteTime"] = _drGridView["CompleteTime"].ToString().Replace("nbsp;", "");
            _drGridView["WorkItem"] = _drGridView["WorkItem"].ToString().Replace("&", "");
            _drGridView["WorkItem"] = _drGridView["WorkItem"].ToString().Replace("nbsp;", "");

            _drGridView["MasterDepartment"] = _drGridView["MasterDepartment"].ToString().Replace("&", "");
            _drGridView["MasterDepartment"] = _drGridView["MasterDepartment"].ToString().Replace("nbsp;", "");
            _drGridView["MasterPeople"] = _drGridView["MasterPeople"].ToString().Replace("&", "");
            _drGridView["MasterPeople"] = _drGridView["MasterPeople"].ToString().Replace("nbsp;", "");
            _drGridView["MasterPeople"] = _drGridView["MasterPeople"].ToString().Replace(",<br>", ",");
            _drGridView["SlaveDepartment"] = _drGridView["SlaveDepartment"].ToString().Replace("&", "");
            _drGridView["SlaveDepartment"] = _drGridView["SlaveDepartment"].ToString().Replace("nbsp;", "");
            _drGridView["SlaveDepartment"] = _drGridView["SlaveDepartment"].ToString().Replace(",<br>", ",");

            _drGridView["SlaveDepartmentDuty"] = _drGridView["SlaveDepartmentDuty"].ToString().Replace("&", "");
            _drGridView["SlaveDepartmentDuty"] = _drGridView["SlaveDepartmentDuty"].ToString().Replace("nbsp;", "");
            _drGridView["EvaluatePeople"] = _drGridView["EvaluatePeople"].ToString().Replace("&", "");
            _drGridView["EvaluatePeople"] = _drGridView["EvaluatePeople"].ToString().Replace("nbsp;", "");
            _drGridView["EvaluatePeople"] = _drGridView["EvaluatePeople"].ToString().Replace(",<br>", ",");

            _drGridView["Remart"] = _drGridView["Remart"].ToString().Replace("&", "");
            _drGridView["Remart"] = _drGridView["Remart"].ToString().Replace("nbsp;", "");
            #endregion

            _quarterPlan.Rows.Add(_drGridView);


        }
        //获取当前添加的内容

        DataRow _drTable = _quarterPlan.NewRow();
        _drTable["QuarterPlanSortId"] = this.txtQuarterPlanSortId.Value.Trim();
        _drTable["StartTime"] = this.txtStartTime.Text.Trim();
        _drTable["CompleteTime"] = this.txtCompleteTime.Text.Trim();
        _drTable["WorkItem"] = this.txtWorkItem.Text;
        _drTable["MasterDepartment"] = this.drpDepartment.SelectedItem.Text;
        _drTable["MasterPeople"] = this.txtMasterPeople.Value.Trim();
        _drTable["SlaveDepartment"] = this.txtSlaveDepartment.Value.Trim();
        _drTable["SlaveDepartmentDuty"] = this.txtSlaveDepartmentDuty.Text.Trim();
        _drTable["EvaluatePeople"] = this.txtVerifyPeople.Value.Trim();
        _drTable["Remart"] = this.txtRemark.Text;
        _quarterPlan.Rows.Add(_drTable);
        DataView dv = new DataView(_quarterPlan);
        dv.Sort = "QuarterPlanSortId";
        this.gdvQuarterPlan.DataSource = dv;
        this.gdvQuarterPlan.DataBind();
        //隐藏首行，只保留表头
        this.gdvQuarterPlan.Rows[0].Visible = false;
        for (int i = 0; i < this.gdvQuarterPlan.Rows.Count; i++)
        {
            wrapByComma(this.gdvQuarterPlan, i, 6);
            wrapByComma(this.gdvQuarterPlan, i, 7);
        }
        #region 善后操作
        //将文本框清空
        this.txtQuarterPlanSortId.Value = this.gdvQuarterPlan.Rows.Count.ToString();
        this.txtCompleteTime.Text = "";
        this.txtMasterPeople.Value = "";
        this.txtRemark.Text = "";
        this.txtSlaveDepartment.Value = "";
        this.txtSlaveDepartmentDuty.Text = "";
        this.txtStartTime.Text = "";
        this.txtVerifyPeople.Value = "";
        this.txtWorkItem.Text = "";
        #endregion
    }


    /// <summary>
    /// 将Excel的计划导入
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnImport_Click(object sender, EventArgs e)
    {
        //得到参数 部门名称的DataTable
        object[] _valueDepartmentName = new object[1];
        _valueDepartmentName[0] = this.drpDepartment.SelectedItem.Text;
        DataTable _VerifyCircuitId = dllVerifyCircuitManager.getVerifyCircuitIdByDepartment(_valueDepartmentName);
        if (_VerifyCircuitId.Rows.Count > 0)
        {
            string _url = "ImportQuarterPlan.aspx?QuarterDate=" + this.drpQuarterDate.SelectedValue + "&VerifyCircuitId=" + Convert.ToInt32(_VerifyCircuitId.Rows[0]["VerifyCircuitId"].ToString());
            Page.RegisterStartupScript("windowopen", "<script>window.open('" + _url + "','','height=160,width=900,top=300,left=300,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no');</script>");
            return;
        }
    }
}
